package com.shenyang.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import com.shenyang.dao.Word;

/**
 * Servlet Filter implementation class CharEncodingFilter
 */
@WebFilter("/CharEncodingFilter")
public class CharEncodingFilter implements Filter {
    private String enCoding=null;
    /**
     * Default constructor. 
     */
    public CharEncodingFilter() {
        System.out.println("CharEncodingFilter#我是过滤器，我调用了构造方法进行了对象初始化");
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		System.out.println("CharEncodingFilter#--->destroy");
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("CharEncodingFilter#/init()--->start");
		request.setCharacterEncoding(this.enCoding);
		response.setContentType("text/html");
		response.setCharacterEncoding(this.enCoding);
		
		Word request2=new Word((HttpServletRequest) request);
	
		
		chain.doFilter(request2, response);
		System.out.println("CharEncodingFilter#init()--->end");
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		System.out.println("CharEncodingFilter#init()-->是构造方法之后执行的");
	    this.enCoding=fConfig.getInitParameter("enCoding");
	    System.out.println("this.enCoding-->"+this.enCoding);
	}

}
